package com.hs.security.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.security.exception.ProjectException;
import com.hs.security.mapper.TbUserRoleMapper;
import com.hs.security.pojo.TbUser;
import com.hs.security.pojo.TbUserRole;
import com.hs.security.service.TbUserRoleService;
import com.hs.security.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郭敖
 * @since 2023-08-18
 */
@Service
public class TbUserRoleServiceImpl extends ServiceImpl<TbUserRoleMapper, TbUserRole> implements TbUserRoleService {

    @Autowired
    private TbUserRoleMapper mapper;


    @Override
    public int removeByUserId(Integer userId) {
        return mapper.deleteByUserId(userId);
    }

    @Override
    public int saveByUserId(TbUser user) {
        int insert = mapper.insertByUserId(user);
        if(insert < 1) {
            throw new ProjectException(Constant.CREATED_ERR.getMsg(), Constant.CREATED_ERR.getCode());
        }
        return insert;
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public Boolean updateUserRoles(TbUser user) {
        mapper.deleteByUserId(user.getId());
        int insert = mapper.insertByUserId(user);
        if (insert < 1){
            throw new ProjectException(Constant.CREATED_ERR.getMsg(),Constant.CREATED_ERR.getCode());
        }
        return true;
    }

}
